// runcompressorDlg.cpp : implementation file
//

#include "stdafx.h"
#include "runcompressor.h"
#include "runcompressorDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CruncompressorDlg dialog



CruncompressorDlg::CruncompressorDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CruncompressorDlg::IDD, pParent)
	, htotalnumber(0)
	, vtotalnumber(0)
	, rootdirectory(_T(""))
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CruncompressorDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control		(pDX, IDC_OUTPUTFILE	, ifoutputcheck);
	DDX_Control		(pDX, IDC_OUTPUTNAME	, output_linectrl);
	DDX_Control		(pDX, IDC_BROWSEOUTPUT	, browseoutputctr);
	DDX_Control		(pDX, IDC_CHECKHTOTAL	, htotalctrl);
	DDX_Control		(pDX, IDC_HTOTAL		, htotalnumberctrl);
	DDX_Control		(pDX, IDC_CHECKVTOTAL	, vtotalctrl);
	DDX_Control		(pDX, IDC_VTOTAL		, vtotalnumberctrl);
	DDX_Text		(pDX, IDC_HTOTAL		, htotalnumber);
	DDV_MinMaxInt	(pDX					, htotalnumber, 0, 10000);
	DDX_Text		(pDX, IDC_VTOTAL		, vtotalnumber);
	DDV_MinMaxInt	(pDX					, vtotalnumber, 0, 10000);
	DDX_Control		(pDX, IDC_EE			, eechecked);
	DDX_Control		(pDX, IDC_EENUMBERS		, eectrl);
	DDX_Control		(pDX, IDC_INPUTFILES	, inputfilesctrl);
	DDX_Control(pDX, IDC_WAVEEDITOR, waveeditorcheck);
	DDX_Control(pDX, IDC_STAT, statctrl);
}

BEGIN_MESSAGE_MAP(CruncompressorDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_OUTPUTFILE, OnBnClickedOutputfile)
	ON_BN_CLICKED(IDC_BROWSEINPUT, OnBnClickedBrowseinput)
	ON_BN_CLICKED(IDC_CHECKHTOTAL, OnBnClickedCheckhtotal)
	ON_BN_CLICKED(IDC_CHECKVTOTAL, OnBnClickedCheckvtotal)
	ON_BN_CLICKED(IDC_EE, OnBnClickedEe)
	ON_BN_CLICKED(IDC_BROWSEOUTPUT, OnBnClickedBrowseoutput)
	ON_BN_CLICKED(IDC_COMPRESS, OnBnClickedCompress)
END_MESSAGE_MAP()


// CruncompressorDlg message handlers

BOOL CruncompressorDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here
	GetCurrentDirectory(1000,rootdirectory.GetBuffer(1000));
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CruncompressorDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CruncompressorDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CruncompressorDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


void CruncompressorDlg::OnBnClickedOutputfile()
{
	// TODO: Add your control notification handler code here
	if(ifoutputcheck.GetCheck()	==	BST_CHECKED)
	{
		output_linectrl.EnableWindow();
		browseoutputctr.EnableWindow();
	}
	else
	{
		output_linectrl.EnableWindow(FALSE);
		browseoutputctr.EnableWindow(FALSE);
	}
	
}


void CruncompressorDlg::OnBnClickedCheckhtotal()
{
	// TODO: Add your control notification handler code here
	if(htotalctrl.GetCheck()	==	BST_CHECKED)
	{
		htotalnumberctrl.EnableWindow();
	}
	else
	{
		htotalnumberctrl.EnableWindow(FALSE);
	}
}


void CruncompressorDlg::OnBnClickedCheckvtotal()
{
	// TODO: Add your control notification handler code here
	if(vtotalctrl.GetCheck()	==	BST_CHECKED)
	{
		vtotalnumberctrl.EnableWindow();
	}
	else
	{
		vtotalnumberctrl.EnableWindow(FALSE);
	}
}

void CruncompressorDlg::OnBnClickedEe()
{
	// TODO: Add your control notification handler code here
	if(eechecked.GetCheck()	==	BST_CHECKED)
	{
		eectrl.EnableWindow();
	}
	else
	{
		eectrl.EnableWindow(FALSE);
	}
}

void CruncompressorDlg::OnBnClickedBrowseoutput()
{
	// TODO: Add your control notification handler code here
	CString sline;
	char Filters[]= "Bin Files (*.bin)|*.bin|All Files (*.*)|*.*||";
	output_linectrl.GetLine(0,sline.GetBuffer(1000),1000);
	CFileDialog file(FALSE,"bin",sline,OFN_FILEMUSTEXIST| OFN_HIDEREADONLY, Filters);
	if (file.DoModal() == IDCANCEL)	
	{
		return ;
	}
	output_linectrl.SetWindowText(file.GetPathName());
}

void CruncompressorDlg::OnBnClickedBrowseinput()
{
	// TODO: Add your control notification handler code here
	CString sline;
	char Filters[]= "Vector Files (*.txt)|*.txt|All Files (*.*)|*.*||";
	CFileDialog file(TRUE,"txt",sline,OFN_FILEMUSTEXIST| OFN_HIDEREADONLY|OFN_ALLOWMULTISELECT , Filters);
	if (file.DoModal() == IDCANCEL)	
	{
		return ;
	}
	
	POSITION pos	=file.GetStartPosition();
	if(pos == NULL)
	{
		return;
	}
	while(pos != NULL)
	{
		CString  name	=file.GetNextPathName(pos);
		inputfilesctrl.ReplaceSel(" "+name);
	}
}

int CruncompressorDlg::RunCompressor(CString commandline)
{
	FILE *batFile ;
	SetCurrentDirectory(rootdirectory);
	batFile	=	fopen("runcompressor.bat","w");
	if(batFile	!=	NULL)
	{
		fwrite(LPCTSTR(commandline),commandline.GetLength(),1,batFile);
		fclose(batFile);
		do{		
			batFile	=	fopen("runcompressor.bat","r");
		}while(batFile	== NULL);
		fclose(batFile);
		ShellExecute(AfxGetMainWnd()->m_hWnd,NULL,"runcompressor.bat",NULL,NULL,SW_SHOWNORMAL);
		
	}
	return 0;
}

void CruncompressorDlg::OnBnClickedCompress()
{
	// TODO: Add your control notification handler code here
	CString commandname = "utg_compressor.exe ";
	char param[1000];
	if(waveeditorcheck.GetCheck()	==	BST_CHECKED)
	{
		commandname +=" -waveeditor";
	}

	if(eechecked.GetCheck()	==	BST_CHECKED)
	{
		int index	=	eectrl.GetCurSel();
		eectrl.GetLBText(index,param);
		commandname +=" -ee "+CString(param);
	}
	
	if(htotalctrl.GetCheck()	==	BST_CHECKED)
	{
		htotalnumberctrl.GetLine(0,param,1000);
		if(strlen(param) > 1)
		{
			commandname +=" -htotal "+CString(param);
		}
	}
	
	if(vtotalctrl.GetCheck()	==	BST_CHECKED)
	{
		vtotalnumberctrl.GetLine(0,param,1000);
		if(strlen(param) > 1)
		{
			commandname +=" -vtotal "+CString(param);
		}
	}
	
	if(ifoutputcheck.GetCheck()	==	BST_CHECKED)
	{
		output_linectrl.GetLine(0,param,1000);
		if(strlen(param) > 1)
		{
			commandname +=" -output "+CString(param);
		}
	}
	
//-----------------------------------------------------------------------------------------------------
	inputfilesctrl.GetLine(0,param,1000);
	if(strlen(param) > 1)
	{
		commandname +=" -input "+CString(param);
	}
	else
	{
		AfxMessageBox("Error no input files",MB_OK|MB_ICONSTOP);
		return;
	}
//-----------------------------------------------------------------------------------------------------
	if(statctrl.GetCheck()	==	BST_CHECKED)
	{
		commandname +=" -stat";
	}
	else
	{
		commandname +=" -";
	}

	commandname += " >utg_compressor.log";
	RunCompressor(commandname);
}
